import { defHttp } from '/@/utils/http/axios';
import { Modal } from 'ant-design-vue';
import { PlatformConfig } from './model/tenantModel';
import { ConfigEnum } from '/@/enums/httpEnum'
import { Api } from '/@/api/url'

export const listSubTenant = (params) => {
  const headers = {}
  headers[ConfigEnum.TENANT_ID] = params.tenantId
  return defHttp.get({url: Api.listSubTenant, params, headers});
}

export const pageTenant = (params) => {
  return defHttp.get({ url: Api.pageTenant, params });
}


export const listTenant = (params) => {
  return defHttp.get({ url: Api.listAllTenant, params });
}

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdateTenant = (params, isUpdate) => {
  let url = isUpdate ? Api.updateTenant : Api.saveTenant;
  return defHttp.post({ url: url, params });
}

/**
 * 获取租户配置
 * @param params
 */
export const getPlatformConfig = () => {
  return defHttp.get<PlatformConfig>({ url: Api.getTenantConfig });
}

/**
 * 拥有者获取
 */
export const getOwnerTenant = () => {
  return defHttp.get({ url: Api.getOwnerTenant });
}

/**
 * 拥有者更新
 */
export const updateOwnerTenant = (params) => {
  return defHttp.post({ url: Api.updateOwnerTenant, params });
}

/**
 * 查询详情
 * @param params
 */
export const getTenant = (params) => {
  return defHttp.get({ url: Api.getTenant, params });
}

/**
 * 删除
 * @param params
 */
export const deleteTenant = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteTenant, data: params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
}

/**
 * 批量删除租户
 * @param params
 */
export const batchDeleteTenant = (params, handleSuccess) => {
  Modal.confirm({
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.batchDeleteTenant, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
}
